package com.bangdream.michelia.tool.retrofithttp.picasso;

import android.content.Context;
import com.squareup.picasso.Picasso;
import com.bangdream.michelia.utils.PublicUtil;

import java.io.File;

import okhttp3.Cache;
import okhttp3.OkHttpClient;

/**
 * Created by Administrator on 2017/2/24.
 * 绑定OkHttp的Picasso框架
 */

public class OkHttpPicasso {
	private static Picasso mPicasso;
	public static Picasso getPicasso(Context context) {
		if(mPicasso==null) {
			OkHttpClient client = getProgressBarClient(context);
			return new Picasso.Builder(context)
					//设置下载器 下载器中设置了cookie
					.downloader(new ImageDownLoader(context, client))
					.build();
		}
		else
		{
			return mPicasso;
		}
	}

	/**
	* Not singleton
	*/
	private  static OkHttpClient getProgressBarClient(Context context) {
		OkHttpClient client=new OkHttpClient
				.Builder()
				.cache(new Cache(new File(PublicUtil.LocalPath+PublicUtil.LocalImageCachePath), 1000*1024))
				.addInterceptor(new CaheInterceptor(context))
				.addNetworkInterceptor(new CaheInterceptor(context))
				.build();
		return client.newBuilder()
				.addInterceptor(new CaheInterceptor(context))
				.addNetworkInterceptor(new CaheInterceptor(context))
				.build();
	}
}
